<!-- @module travel-form -->
<!-- @author: YunTao.Li -->
<!-- @description:  出差单form组件-->
<!-- @since: 2019-11-19 12:47:36 -->
<template>
  <!--1级弹框 begin-->
  <el-dialog class="dialogStyle" title="出差申请单" :visible.sync="dialogFormVisible" width="90%" top="5vh" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
    <!--1级弹框 form begin-->
    <fieldset style="border:none">
      <el-form ref="travelInputForm" :model="travelInputForm" :label-position="labelPosition" :rules="formFormatRules" label-width="120px" size="mini">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="组织机构" prop="orgCode">
              <OrgSelectTree ref="orgSelectRef" v-model="travelInputForm.orgCode" placeholder="组织机构" :disabled="true" @bindLabel="bindLabel" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="travelInputForm.deptName" name="deptName" placeholder="部门名称" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="申请人" prop="empName">
              <el-input v-model="travelInputForm.empName" placeholder="申请人" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="申请单编码" prop="bizTravelCode">
              <el-input v-model="travelInputForm.bizTravelCode" name="bizTravelCode" placeholder="保存后自动生成" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker v-model="travelInputForm.startDate" :picker-options="pickerOptionsStart" style="width:100%;" type="date" name="startDate" placeholder="开始日期" value-format="yyyy-MM-dd HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="开始时段" prop="startTimeType">
              <!-- <el-input v-model="travelInputForm.startTimeType" name="startTimeType" placeholder="开始时段" /> -->
              <dictCombo v-model="travelInputForm.startTimeType" dicttypeid="timeTypeChose" name="startTimeType" placeholder="开始时段" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker v-model="travelInputForm.endDate" :picker-options="pickerOptionsEnd" style="width:100%;" type="date" name="endDate" placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="结束时段" prop="endTimeType">
              <!-- <el-input v-model="travelInputForm.endTimeType" name="endTimeType" placeholder="结束时段" /> -->
              <dictCombo v-model="travelInputForm.endTimeType" dicttypeid="timeTypeChose" name="endTimeType" placeholder="结束时段" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="交通工具" prop="traffic">
              <!-- <el-input v-model="travelInputForm.traffic" name="traffic" placeholder="交通工具" /> -->
              <dictCombo v-model="travelInputForm.traffic" dicttypeid="traffic" name="traffic" placeholder="交通工具" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="返回交通工具" prop="backTraffic">
              <!-- <el-input v-model="travelInputForm.backTraffic" name="backTraffic" placeholder="返回交通工具" /> -->
              <dictCombo v-model="travelInputForm.backTraffic" dicttypeid="traffic" name="traffic" placeholder="交通工具" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="出差天数" prop="days">
              <el-input v-model="travelInputForm.days" name="days" placeholder="出差天数" :disabled="true" />
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="实际天数" prop="actualDays">
              <el-input v-model="travelInputForm.actualDays" name="actualDays" placeholder="实际天数" />
            </el-form-item>
          </el-col> -->
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
            <el-form-item label="出差路线" prop="trace">
              <el-input v-model="travelInputForm.trace" name="trace" placeholder="出差路线" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="出差待办事由" prop="travelDesc">
              <el-input v-model="travelInputForm.travelDesc" type="textarea" name="travelDesc" style="width:80%" placeholder="出差待办事由" rows="2" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </fieldset>

    <!-- 随同人员 begin-->
    <fieldset class="fieldset" style="margin-top:5px;">
      <!--button-->
      <!-- <div class="div-button" style="margin-top:10px;">
        <el-button
          icon="el-icon-menu"
          type="success"
          size="mini"
          disabled="true"
          @click="onAddEmp"
        >新增</el-button>
      </div> -->
      <legend class="legend-font" v-text="travel_emp_text" />
      <div class="div-table">
        <fieldset class="fieldset" style="margin-top:15px;border:none !important">
          <legend class="legend-font" />
          <el-table ref="travelEmpTable" :data="travelInputForm.bizTravelEmpList" :v-model="travelInputForm.bizTravelEmpList" border fit highlight-current-row style="width: 100%" max-height="250" size="mini">
            <el-table-column type="selection" width="55" />
            <el-table-column align="center" type="index" width="55" label="序号" />
            <!--员工名称-->
            <el-table-column align="center" label="员工名称" width="150px" prop="empName">
              <template slot-scope="scope">
                <!-- <el-input v-model="scope.row.empName" size="mini" placeholder="员工名称" /> -->
                <span>{{ scope.row.empName }}</span>
              </template>
            </el-table-column>
            <!--员工号-->
            <el-table-column align="center" label="员工号" width="150px" prop="empId">
              <template slot-scope="scope">
                <!-- <el-input v-model="scope.row.empId" size="mini" placeholder="员工编码" /> -->
                <span>{{ scope.row.empId }}</span>
              </template>
            </el-table-column>
            <!--开始日期-->
            <el-table-column align="center" label="开始日期" prop="startDate">
              <template slot-scope="scope">
                <el-date-picker v-model="scope.row.startDate" style="width:100%;" type="date" name="startDate" placeholder="开始日期" />
              </template>
            </el-table-column>
            <!--开始时段-->
            <el-table-column width="100px" align="center" label="开始时段" prop="startTimeType">
              <template slot-scope="scope">
                <dictCombo v-model="scope.row.startTimeType" dicttypeid="timeTypeChose" name="startTimeType" placeholder="开始时段" />
              </template>
            </el-table-column>
            <!--结束日期-->
            <el-table-column align="center" label="结束日期" prop="endDate">
              <template slot-scope="scope">
                <el-date-picker v-model="scope.row.endDate" style="width:100%;" type="date" name="endDate" placeholder="结束日期" />
              </template>
            </el-table-column>
            <!--结束时段-->
            <el-table-column width="100px" align="center" label="结束时段" prop="endTimeType">
              <template slot-scope="scope">
                <!-- <span>{{ scope.row.endTimeType }}</span> -->
                <dictCombo v-model="scope.row.endTimeType" dicttypeid="timeTypeChose" name="endTimeType" placeholder="结束时段" />
              </template>
            </el-table-column>
            <!-- <el-table-column label="操作">
              <template scope="scope">
                <el-button size="small" type="danger" disabled="true" @click="onDelEmp(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column> -->
          </el-table>
        </fieldset>
      </div>
    </fieldset>
    <!-- 随同人员 end-->

    <!--file begin-->
    <!-- <fileUpload :param-pa-doc-id="travelInputForm.fileParamPaDocId" /> -->
    <!--file end-->

    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button size="mini" @click="onCancel">取 消</el-button>
      <el-button type="primary" size="mini" @click="onSubmit">保存</el-button>
    </div>
  </el-dialog>
  <!--1级弹框 end-->

</template>

<script>
// import fileUpload from './../fileupload'
import dictCombo from './../combo/dict-comb'
import { travelEmps } from '@/api/attendance/travel-service'
import OrgSelectTree from './../combo/org-tree-select.vue'
import { createUniqueString } from '@/utils'
import { dateCalc } from '@/api/attendance/holidaylaws-service'

export default {
  components: { dictCombo, OrgSelectTree },
  data() {
    return {
      dialogFormVisible: false,
      labelPosition: 'right',
      travel_emp_text: '随同人员',
      btn_emp_del_disabled: false,
      travelInputForm: {
        fileParamPaDocId: '',
        bizTravelId: '',
        ouId: '',
        orgCode: '',
        orgName: '',
        deptId: '',
        createdBy: '',
        startDate: null,
        startTimeType: '',
        endDate: null,
        endTimeType: '',
        traffic: '',
        backTraffic: '',
        days: '',
        bizTravelCode: '',
        actualDays: '',
        trace: '',
        travelDesc: '',
        bizTravelEmpList: [{
          empName: '',
          empId: '',
          startDate: '',
          startTimeType: '',
          endTimeType: ''
        }]
      },
      pickerOptionsStart: {
        disabledDate: (time) => {
          if (this.travelInputForm.endDate) {
            return time.getTime() > new Date(this.travelInputForm.endDate).getTime() - 1 * 24 * 60 * 60 * 1000
          }
          return false
        }
      },
      pickerOptionsEnd: {
        disabledDate: (time) => {
          return time.getTime() < new Date(this.travelInputForm.startDate).getTime() + 1 * 24 * 60 * 60 * 1000
        }
      },
      dateCalcQuery: {
        startDate: null,
        startTimeType: '',
        endDate: null,
        endTimeType: ''
      },
      formFormatRules: {
        startDate: [{
          required: true,
          message: '开始日期不能为空',
          trigger: 'blur'
        }],
        startTimeType: [{
          required: true,
          message: '开始时段不能为空',
          trigger: 'blur'
        }],
        endDate: [{
          required: true,
          message: '结束日期不能为空',
          trigger: 'blur'
        }],
        endTimeType: [{
          required: true,
          message: '结束时段不能为空',
          trigger: 'blur'
        }]
      },
      formLabelWidth: '120px'
    }
  },
  computed: {
    cmp_startDate() {
      return this.travelInputForm.startDate
    },
    cmp_startTimeType() {
      return this.travelInputForm.startTimeType
    },
    cmp_endDate() {
      return this.travelInputForm.endDate
    },
    cmp_endTimeType() {
      return this.travelInputForm.endTimeType
    }
  },
  watch: {
    cmp_startDate(newValue, oldValue) {
      this.daysCalc()
    },
    cmp_startTimeType(newValue, oldValue) {
      this.daysCalc()
    },
    cmp_endDate(newValue, oldValue) {
      this.daysCalc()
    },
    cmp_endTimeType(newValue, oldValue) {
      this.daysCalc()
    }
  },
  created() {
    this.travelInputForm.bizTravelEmpList.splice(0, this.travelInputForm.bizTravelEmpList.length)
  },
  methods: {
    bindLabel(orgId, label) {
      if (orgId && orgId !== '') {
        this.leaveInputForm.ouId = orgId
      }

      if (label && label !== '') {
        this.leaveInputForm.orgName = label
      }
    },
    daysCalc() {
      this.dateCalcQuery.startDate = this.travelInputForm.startDate
      this.dateCalcQuery.startTimeType = this.travelInputForm.startTimeType
      this.dateCalcQuery.endDate = this.travelInputForm.endDate
      this.dateCalcQuery.endTimeType = this.travelInputForm.endTimeType

      if (this.dateCalcQuery.startDate && this.dateCalcQuery.startTimeType && this.dateCalcQuery.endDate && this.dateCalcQuery.endTimeType) {
        dateCalc(this.dateCalcQuery).then(response => {
          const data = response.data
          this.travelInputForm.days = data.workDay
          this.travelInputForm.actualDays = data.natureDay
        })
      } else {
        console.log('value null')
      }
    },
    toggele(data) { // 显示此窗口或隐藏
      if (this.dialogFormVisible) {
        this.dialogFormVisible = false
      } else {
        this.dialogFormVisible = true
      }

      if (data && data != null) {
        this.$nextTick(function() {
          this.travelInputForm = data
          debugger

          //* ***file begin */
          this.leaveInputForm.fileParamPaDocId = this.leaveInputForm.leaveId
          /** **file end */
        })

        const bizTravelId = data.bizTravelId
        this.onLoadTravelEmps(bizTravelId)
      } else {
        //* ***file begin */
        if (this.dialogFormVisible === true) {
          const uuid = createUniqueString()
          this.leaveInputForm.fileParamPaDocId = uuid
        }
        /** **file end */
      }
    },
    onCancel() { // 取消
      this.toggele()
      this.resetFormData()
    },
    onSubmit() { // 保存
      this.dialogFormVisible = false
      this.$emit('submitFunc', this.travelInputForm)
    },
    onAddEmp() {
      this.$refs.travelInputForm.validate((valid) => {
        if (valid) {
          // 构造form对象用于填充人员信息
          const formStartDate = this.travelInputForm.startDate
          const formStartTimeType = this.travelInputForm.startTimeType
          const formEndDate = this.travelInputForm.endDate
          const formEndTimeType = this.travelInputForm.endTimeType

          const travelEmpInfo = {
            empId: '10050271',
            empName: '王喜升',
            startDate: formStartDate,
            startTimeType: formStartTimeType,
            endDate: formEndDate,
            endTimeType: formEndTimeType
          }

          this.travelInputForm.bizTravelEmpList.push(travelEmpInfo)
          setTimeout(() => { this.$refs.travelEmpTable.setCurrentRow(travelEmpInfo) }, 10)
        } else {
          return false
        }
      })
    },
    onDelEmp(index, row) {
      this.travelInputForm.bizTravelEmpList.splice(index, 1)
    },
    onLoadTravelEmps(travelId) {
      travelEmps(travelId).then(response => {
        this.travelInputForm.bizTravelEmpList = response.data
      })
    },
    resetFormData() {
      this.$refs.travelInputForm.resetFields()
      this.travelInputForm.bizTravelEmpList.splice(0, this.travelInputForm.bizTravelEmpList.length)
      this.travelInputForm = {
        fileParamPaDocId: '',
        bizTravelId: '',
        ouId: '',
        orgCode: '',
        orgName: '',
        deptId: '',
        createdBy: '',
        startDate: new Date(),
        startTimeType: '',
        endDate: new Date(),
        endTimeType: '',
        traffic: '',
        backTraffic: '',
        days: '',
        bizTravelCode: '',
        actualDays: '',
        trace: '',
        travelDesc: '',
        bizTravelEmpList: [{
          empName: '',
          empId: '',
          startDate: '',
          startTimeType: '',
          endTimeType: ''
        }]
      }

      this.travelInputForm.orgName = ''
      this.$refs.orgSelectRef.clearHandle()
    }
  }
}
</script>

<style>
/* .el-date-picker{
  width: 80%
} */
</style>
